package com.spyx.acl.controller;

import com.atyx.spyx.model.acl.Admin;
import com.atyx.spyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spyx.acl.service.AdminRoleService;
import com.spyx.acl.service.AdminService;
import com.spyx.common.MD5;
import com.spyx.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags="用户管理")
//@CrossOrigin
@RequestMapping("/admin/acl/user")
public class AdminController {
    @Autowired
    private AdminService adminService;

    //分页+模糊查
    @GetMapping("/{page}/{limit}")
    @ApiOperation("用户列表查询")
    public Result getList(@PathVariable("page")Integer page,
                          @PathVariable("limit")Integer limit,
                           AdminQueryVo queryVo){
        //1.封装分页数据
        Page p1 = new Page(page,limit);
        //2.调取service层方法
       IPage<Admin> iPage= adminService.selectList(p1,queryVo);

        return Result.ok(iPage);
    }


    @GetMapping("/get/{id}")
    @ApiOperation("用户的主键查询")
    public Result getById(@PathVariable("id")Integer uid){
        Admin admin = adminService.getById(uid);
        return Result.ok(admin);
    }

    @PostMapping("/save")
    @ApiOperation("新增用户")
    public Result save(@RequestBody Admin admin){

        String s = MD5.encrypt(admin.getPassword());
        admin.setPassword(s);
        adminService.save(admin);
        return Result.ok();
    }

    //更新用户
    @PutMapping("/update")
    @ApiOperation("更新用户")
    public Result update(@RequestBody Admin admin){
        //注意:如果更新的内容中有密码，需要对密码加密后再更新
        adminService.updateById(admin);
        return Result.ok();
    }

    //删除用户
    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除用户")
    public Result deleteById(@PathVariable("id") Integer uid){
        adminService.removeById(uid);
        return Result.ok();
    }

    //删除多个用户
    @DeleteMapping("/batchRemove")
    @ApiOperation("删除多个用户")
    public Result deleteusers(@RequestBody List<Integer> ids){
        adminService.removeByIds(ids);
        return Result.ok();
    }


    @Autowired
    private AdminRoleService adminRoleService;

    //获取某个用户的角色和所有角色
    @GetMapping("/toAssign/{adminId}")
    @ApiOperation("获取某个用户的角色列表和所有角色")
    public Result getRolesById(@PathVariable("adminId") Integer adminId){
        //根据用户id查询用户的角色列表
        //查询所有的角色表
       Map<String,List> map= adminRoleService.seletRoleList(adminId);

        return Result.ok(map);
    }


    //分配角色      对于key=value的参数获取方式:  (1) 参数名=key值  (2)使用实体类接收，接收时，实体类中的属性名=key
    @PostMapping("doAssign")
    @ApiOperation("分配角色")
    public Result doassign(Long adminId, Long[] roleId){
        //调取分配角色的方法
        adminRoleService.saveRole_Admin(adminId,roleId);
        return Result.ok();
    }

















}
